﻿using HomagChina.Basic.CCT.Db.CCHS_Db;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using Proxy;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class ManualOutfeedViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private SearchInfoHandle Handle_Search;
        private InsertInfoHandle Handle_Insert;
        private ModifyInfoHandle Handle_modify;
        private CommonHandle commonHandle;

        #endregion Handle

        #region 属性

        public RelayCommand ApplyCommand { get; }

        private int pcs;
        private string description;

        public int Pcs
        {
            get => pcs;
            set { pcs = value; OnPropertyChanged(); }
        }

        public string Description
        {
            get => description;
            set { description = value; OnPropertyChanged(); }
        }

        public DropDownList DropDownList_OutfeedPos { get; set; }
        public DropDownList DropDownList_BoardCode { get; set; }
        public DropDownList DropDownList_TargetPostion { get; set; }
        public DropDownList DropDownList_DummyBoard { get; set; }

        #endregion 属性

        public ManualOutfeedViewModel()
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_modify = new ModifyInfoHandle();
            commonHandle = new CommonHandle();
            ApplyCommand = new RelayCommand(Apply);

            //LoadDropDownList();
        }

        private void LoadDropDownList()
        {
            var positionInfos = Handle_Search.GetPositionInfos();
            var lis = new List<string>();
            lis.Add(GetLocalText("System allocated"));
            foreach (var item in positionInfos.Where(s => s.Positiontype == 2).Select(s => s.Positioncode).ToArray())
            {
                lis.Add(item);
            }
            DropDownList_OutfeedPos = new DropDownList
            {
                ItemsSource = lis,
            };
            DropDownList_OutfeedPos.SelectedItem = DropDownList_OutfeedPos.Items.CurrentItem;
            DropDownList_OutfeedPos.SelectedIndex = 0;

            var materialList = Handle_Search.GetStockInfos().GroupBy(s => s.Material).ToList();
            var dropDownList = new List<string>();
            materialList.ForEach(s =>
            {
                if (!string.IsNullOrWhiteSpace(s.Key))
                {
                    dropDownList.Add(s.Key);
                }
            });

            DropDownList_TargetPostion = new DropDownList
            {
                ItemsSource = positionInfos.Where(s => s.Positiontype != 0).Select(s => s.Positioncode).ToArray(),
                SelectedIndex = 0
            };
            DropDownList_TargetPostion.SelectedItem = DropDownList_TargetPostion.Items.CurrentItem;
            DropDownList_TargetPostion.SelectionChanged += HandleSelectedChange;

            DropDownList_BoardCode = new DropDownList
            {
                ItemsSource = dropDownList,
                SelectedIndex = 0
            };
            DropDownList_BoardCode.SelectedItem = DropDownList_BoardCode.Items.CurrentItem;

            DropDownList_DummyBoard = new DropDownList
            {
                ItemsSource = (new List<string>() { { GetLocalText("Need") }, { GetLocalText("Not Need") } }),//, { GetLocaText("Not Need") }
                SelectedIndex = 0
            };
            DropDownList_DummyBoard.SelectedItem = DropDownList_DummyBoard.Items.CurrentItem;
        }

        private void HandleSelectedChange(object sender, SelectionChangedEventArgs e)
        {
            T_CCHS_Stock stockInfo = Handle_Search.GetStockInfos().FirstOrDefault(s => s.Positioncode.Equals(DropDownList_TargetPostion.SelectedItem));
            if (stockInfo != null)
            {
                string material = stockInfo.Material;
                DropDownList_BoardCode.SelectedItem = material == "" ? null : material;
            }
        }

        public void Apply()
        {
            var outfeedPos = DropDownList_OutfeedPos.SelectedValue.ToString();
            var TargetPosition = DropDownList_TargetPostion.SelectedValue.ToString();
            var Material = DropDownList_BoardCode.SelectedValue.ToString(); ;
            var re = Handle_Insert.ManuallyOutfeed(Material, outfeedPos, TargetPosition, pcs, DropDownList_DummyBoard.SelectedValue.ToString() == GetLocalText("Need") ? true : false);
            HomagGroup.Base.UI.Windows.MessageBox.Show(re.Msg);
            commonHandle.CloseDiaLog(GetLocalText("Manual Outfeed"));
        }
    }
}